﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileSales.Website.EmployeeService;
using MobileSales.Model.Employees;
using MobileSales.Website.Infrastructure.Messaging;
using System.ServiceModel;
using MobileSales.Model.Exceptions;
using MobileSales.Common.Logger;
using MobileSales.Website.Infrastructure;
using MobileSales.Website.Localization;

namespace MobileSales.Website.Controllers
{
    [Authorize]
    public class ContractsController : ControllerBase
    {
        #region Index
        //
        // GET: /Contracts/

        public ActionResult Index()
        {
            EmployeeServiceClient employeeService = new EmployeeServiceClient();
            employeeService.AddValidationToken(this.Token);
            Contract[] contracts = employeeService.GetContractList();
            employeeService.Close();
            if (contracts == null)
                this.SetMessage(ErrorStrings.ErrorContractList, MessageType.Error);
            return View(contracts);
        } 
        #endregion

        #region Details
        //
        // GET: /Contracts/Details/5

        public ActionResult Details(int id)
        {
            EmployeeServiceClient employeeService = new EmployeeServiceClient();
            employeeService.AddValidationToken(this.Token);
            Contract contract = employeeService.GetContract(id);
            employeeService.Close();
            if (contract == null)
                this.SetMessage(ErrorStrings.ErrorContract, MessageType.Error);
            return View(contract);
        } 
        #endregion

        #region Create
        //
        // GET: /Contracts/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Contracts/Create

        [HttpPost]
        public ActionResult Create(Contract contract)
        {
            try
            {
                EmployeeServiceClient employeeService = new EmployeeServiceClient();
                employeeService.AddValidationToken(this.Token);
                employeeService.StoreContract(contract);
                employeeService.Close();
                this.SetMessage(InfoStrings.ContractCreated, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorContractCreating, MessageType.Error);
            }
            return View(contract);
        } 
        #endregion

        #region Edit
        //
        // GET: /Contracts/Edit/5

        public ActionResult Edit(int id)
        {
            return Details(id);
        }

        //
        // POST: /Contracts/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Contract contract)
        {
            try
            {
                EmployeeServiceClient employeeService = new EmployeeServiceClient();
                employeeService.AddValidationToken(this.Token);
                contract.Id = id;
                employeeService.StoreContract(contract);
                employeeService.Close();
                this.SetMessage(InfoStrings.ContractUpdated, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorContractUpdating, MessageType.Error);
            }
            return View(contract);
        } 
        #endregion

        #region Delete
        //
        // GET: /Contracts/Delete/5

        public ActionResult Delete(int id)
        {
            return Details(id);
        }

        //
        // POST: /Contracts/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                EmployeeServiceClient employeeService = new EmployeeServiceClient();
                employeeService.AddValidationToken(this.Token);
                employeeService.DeleteContract(id);
                employeeService.Close();
                this.SetMessage(InfoStrings.ContractDeleted, MessageType.Info);
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorContractDeleting, MessageType.Error);
            }
            return RedirectToAction("Index");
        } 
        #endregion
    }
}
